Skip to content

Comments

Allow the leader election resource lock type to be configurable#1448

Closed
mprahl wants to merge 1 commit intoopenshift:masterfrom
mprahl:leader-election
Closed

Allow the leader election resource lock type to be configurable#1448
mprahl wants to merge 1 commit intoopenshift:masterfrom
mprahl:leader-election

Conversation

@mprahl
Copy link

@mprahl mprahl commented Jan 6, 2023

The controllercmd package always utilized a configmapsleases resource lock for leader election, but some client may entirely want to move to leases. This commit keeps the default to configmapsleases but allows it to be configurable to whatever the
k8s.io/client-go/tools/leaderelection/resourcelock package allows.

Related issues:
#1178
https://issues.redhat.com/browse/ACM-2653

@mprahl
Copy link
Author

mprahl commented Jan 6, 2023

/cc deads2k soltysh sttts

@openshift-ci openshift-ci bot requested review from deads2k, soltysh and sttts January 6, 2023 15:13
@mprahl
Copy link
Author

mprahl commented Jan 13, 2023

Could I please get a review on this PR?

@mprahl mprahl requested review from dgrisonnet and removed request for deads2k, soltysh and sttts January 18, 2023 16:08
@mprahl
Copy link
Author

mprahl commented Jan 26, 2023

@dgrisonnet could you please provide your thoughts on my last comment?

@mprahl mprahl requested review from dgrisonnet and tkashem and removed request for dgrisonnet February 3, 2023 18:58
@mprahl
Copy link
Author

mprahl commented Feb 3, 2023

Okay, the PR is updated.

@mprahl mprahl requested review from dgrisonnet and removed request for tkashem February 6, 2023 13:16
@mprahl
Copy link
Author

mprahl commented Feb 6, 2023

@dgrisonnet is this good to merge then?

// See https://github.com/kubernetes/kubernetes/issues/107454 for
// details on how to migrate to "leases" leader election.
// Don't forget the callbacks!
// TODO: In the next version we should switch to using "leases"
Copy link
Member

@dgrisonnet dgrisonnet Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkashem did you intend to switch the default to leases instead of configmapleases? If so we might want to keep this TODO until the default resource is updated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dgrisonnet and @tkashem I moved the TODO to where the logic for the default resource lock is made. Is this okay with you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, actually, that was the goal

we want the following:

  • A: a brand new operator being written should us lease (preferably by default)
  • B: existing operators who are currently using configmapsleases should use lease (preferably by default)
  • C: existing operators who have not yet upgraded libray-go for a while (still using configmaps) should start using configmapsleases as soon as they upgrade to the latest library-go (we can't have these operators use lease by default, it will break their upgrade flow)

I am concerned about C - if we don't have any existing operators in C category today then we can safely rename ToLeaderElectionWithConfigmapLease to ToLeaderElectionWithLease and have lease be the default option.
So I would like to know if C is empty today - it can be possible today to count C, maybe by having a test in CI?

or is there a way we can break (static, compile time) these operators in C when they upgrade library-go so they can opt-in? I would like us to be able to avoid these changes if possible, especially if C is empty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leases are not 100% reliable today. It's been a while since the switch I think, I'd simply switch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mprahl can you revise the pr to switch to lease? (disregard C, that means no need to make anything configurable)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkashem in ACM we would like to still have the option for configmapsleases if that's okay. I'd make lease the default though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mprahl yeah, sure, go ahead. (just curious, you have an operator that is currently using configmaps?)

The controllercmd package always utilized a configmapsleases resource
lock for leader election, but some client may entirely want to move to
leases. This commit keeps the default to configmapsleases but allows it
to be configurable of current best practices.

Related issues:
#1178
https://issues.redhat.com/browse/ACM-2653

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Feb 13, 2023

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mprahl
Once this PR has been reviewed and has the lgtm label, please assign sttts for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mprahl
Copy link
Author

mprahl commented Feb 13, 2023

/retest


// WithLeaderElectionResourceLock sets the resource lock. If not set, controllercmd.ConfigMapsLeasesResourceLock will
// be used for backwards compatibility.
func (b *ControllerBuilder) WithLeaderElectionResourceLock(resourceLock leaderElectionResourceLock) *ControllerBuilder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of using the lock as a parameter, can we have two methods?

WithLeaderElectionLeaseResourceLock() *ControllerBuilder
WithLeaderElectionConfigmapLeaseResourceLock() *ControllerBuilder

this way we don't have to have validation around resource lock names and in future we can simply remove WithLeaderElectionConfigmapLeaseResourceLock

// ToLeaderElectionWithLease returns a "leases" based leader
// election config that you just need to fill in the Callback for.
// Don't forget the callbacks!
func ToLeaderElectionWithLease(clientConfig *rest.Config, config configv1.LeaderElection, component, identity string) (leaderelection.LeaderElectionConfig, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove ToLeaderElectionWithLease and restore toLeaderElection to ToLeaderElection

return fmt.Errorf("%w: %s", ErrInvalidResourceLock, b.leaderElectionResourceLock)
}

leaderElection, err := leaderelectionconverter.ToLeaderElectionWithConfigmapLease(leaderConfig, *b.leaderElection, b.componentName, b.instanceIdentity)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use proposed eaderelectionconverter.ToLeaderElection(leaderConfig, *b.leaderElection, b.componentName, b.instanceIdentity, b.leaderElectionResourceLock

we can get rid of the switch here


// LeaderElectionResourceLock sets the resource lock. If not set, controllercmd.ConfigMapsLeasesResourceLock will
// be used for backwards compatibility.
LeaderElectionResourceLock leaderElectionResourceLock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to know whether it's lease or configmapsleases, so can we make it a boolean

// DEPRECATED: at some point everything should uses lease by default
OverrideConfigmapsLeasesResourceLock bool

This will allow new operators to opt in for lease

WithKubeConfigFile(c.basicFlags.KubeConfigFile, nil).
WithComponentNamespace(c.basicFlags.Namespace).
WithLeaderElection(config.LeaderElection, c.basicFlags.Namespace, c.componentName+"-lock").
WithLeaderElectionResourceLock(c.LeaderElectionResourceLock).
Copy link
Contributor

@tkashem tkashem Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace it with

builder = builder. WithLeaderElectionConfigmapLeaseResourceLock() 
if c.OverrideConfigmapsLeasesResourceLock {
     builder = builder. WithLeaderElectionLeaseResourceLock() 
}

@mprahl
Copy link
Author

mprahl commented Apr 11, 2023

Closing in favor of:
caaf57d

@mprahl mprahl closed this Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants